home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python152_Src.lha / Python152_Source / Parser / intrcheck.c < prev    next >
C/C++ Source or Header  |  1999-04-25  |  5KB  |  250 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Check for interrupts */
  33.  
  34. #include "config.h"
  35.  
  36. /* config.h may or may not define DL_IMPORT */
  37. #ifndef DL_IMPORT    /* declarations for DLL import/export */
  38. #define DL_IMPORT(RTYPE) RTYPE
  39. #endif
  40.  
  41. #include "myproto.h"
  42. #include "mymalloc.h" /* For ANY */
  43. #include "intrcheck.h"
  44. #ifdef HAVE_FCNTL_H
  45. #include <fcntl.h>
  46. #endif
  47.  
  48. /* Copied here from ceval.h -- can't include that file. */
  49. int Py_AddPendingCall Py_PROTO((int (*func) Py_PROTO((ANY *)), ANY *arg));
  50.  
  51.  
  52. #ifdef QUICKWIN
  53.  
  54. #include <io.h>
  55.  
  56. void
  57. PyOS_InitInterrupts()
  58. {
  59. }
  60.  
  61. void
  62. PyOS_FiniInterrupts()
  63. {
  64. }
  65.  
  66. int
  67. PyOS_InterruptOccurred()
  68. {
  69.     _wyield();
  70. }
  71.  
  72. #define OK
  73.  
  74. #endif /* QUICKWIN */
  75.  
  76. #if defined(_M_IX86) && !defined(__QNX__)
  77. #include <io.h>
  78. #endif
  79.  
  80. #if defined(MSDOS) && !defined(QUICKWIN)
  81.  
  82. #ifdef __GNUC__
  83.  
  84. /* This is for DJGPP's GO32 extender.  I don't know how to trap
  85.  * control-C  (There's no API for ctrl-C, and I don't want to mess with
  86.  * the interrupt vectors.)  However, this DOES catch control-break.
  87.  * --Amrit
  88.  */
  89.  
  90. #include <go32.h>
  91.  
  92. void
  93. PyOS_InitInterrupts()
  94. {
  95.     _go32_want_ctrl_break(1 /* TRUE */);
  96. }
  97.  
  98. void
  99. PyOS_FiniInterrupts()
  100. {
  101. }
  102.  
  103. int
  104. PyOS_InterruptOccurred()
  105. {
  106.     return _go32_was_ctrl_break_hit();
  107. }
  108.  
  109. #else /* !__GNUC__ */
  110.  
  111. /* This might work for MS-DOS (untested though): */
  112.  
  113. void
  114. PyOS_InitInterrupts()
  115. {
  116. }
  117.  
  118. void
  119. PyOS_FiniInterrupts()
  120. {
  121. }
  122.  
  123. int
  124. PyOS_InterruptOccurred()
  125. {
  126.     int interrupted = 0;
  127.     while (kbhit()) {
  128.         if (getch() == '\003')
  129.             interrupted = 1;
  130.     }
  131.     return interrupted;
  132. }
  133.  
  134. #endif /* __GNUC__ */
  135.  
  136. #define OK
  137.  
  138. #endif /* MSDOS && !QUICKWIN */
  139.  
  140.  
  141. #ifdef macintosh
  142.  
  143. /* The Mac interrupt code has moved to macglue.c */
  144. #define OK
  145.  
  146. #endif /* macintosh */
  147.  
  148.  
  149. #ifndef OK
  150.  
  151. /* Default version -- for real operating systems and for Standard C */
  152.  
  153. #include <stdio.h>
  154. #include <string.h>
  155. #include <signal.h>
  156. #ifdef HAVE_UNISTD_H
  157. #include <unistd.h>
  158. #endif
  159.  
  160. static int interrupted;
  161.  
  162. void
  163. PyErr_SetInterrupt()
  164. {
  165.     interrupted = 1;
  166. }
  167.  
  168. extern int PyErr_CheckSignals();
  169.  
  170. /* ARGSUSED */
  171. static RETSIGTYPE
  172. #if defined(_AMIGA) || defined(_M_IX86) && !defined(__QNX__)
  173. intcatcher(int sig)    /* So the C compiler shuts up */
  174. #else /* _M_IX86 */
  175. intcatcher(sig)
  176.     int sig; /* Not used by required by interface */
  177. #endif /* _M_IX86 */
  178. {
  179.     extern void Py_Exit Py_PROTO((int));
  180.     static char message[] =
  181. "python: to interrupt a truly hanging Python program, interrupt once more.\n";
  182.     switch (interrupted++) {
  183.     case 0:
  184.         break;
  185.     case 1:
  186.         write(2, message, strlen(message));
  187.         break;
  188.     case 2:
  189.         interrupted = 0;
  190.         Py_Exit(1);
  191.         break;
  192.     }
  193.     signal(SIGINT, intcatcher);
  194.     Py_AddPendingCall(PyErr_CheckSignals, NULL);
  195. }
  196.  
  197. static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
  198.  
  199. void
  200. PyOS_InitInterrupts()
  201. {
  202.     if ((old_siginthandler = signal(SIGINT, SIG_IGN)) != SIG_IGN)
  203.         signal(SIGINT, intcatcher);
  204. #ifdef HAVE_SIGINTERRUPT
  205.     /* This is for SunOS and other modern BSD derivatives.
  206.        It means that system calls (like read()) are not restarted
  207.        after an interrupt.  This is necessary so interrupting a
  208.        read() or readline() call works as expected.
  209.        XXX On old BSD (pure 4.2 or older) you may have to do this
  210.        differently! */
  211.     siginterrupt(SIGINT, 1);
  212. #endif /* HAVE_SIGINTERRUPT */
  213. }
  214.  
  215. void
  216. PyOS_FiniInterrupts()
  217. {
  218.     signal(SIGINT, old_siginthandler);
  219. }
  220.  
  221. int
  222. PyOS_InterruptOccurred()
  223. {
  224. #ifdef __SASC
  225.     extern void __regargs __chkabort(void);
  226.     extern void chkabort(void);
  227.  
  228.     chkabort();        /* explicit Amiga SAS/C ^C check */
  229. #endif
  230.     if (!interrupted)
  231.         return 0;
  232.     interrupted = 0;
  233.     return 1;
  234. }
  235.  
  236. #ifdef __SASC
  237. /* Amiga SAS/C replacement ^C handler */
  238. void __regargs _CXBRK(void)
  239. {
  240.     interrupted=1;
  241. }
  242. #endif
  243.  
  244. #endif /* !OK */
  245.  
  246. void
  247. PyOS_AfterFork()
  248. {
  249. }
  250.